<template>
  <div class="payment-page">
    <div class="payment-container">
      <!-- Header -->
      <div class="header">
        <h1>订单支付</h1>
        <div class="timer">
          <span class="timer-icon">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <circle cx="12" cy="12" r="10"></circle>
              <polyline points="12 6 12 12 16 14"></polyline>
            </svg>
          </span>
          <span>支付剩余时间: {{ formatTime }}</span>
        </div>
      </div>

      <!-- Amount Display -->
      <div class="amount-display">
        <div class="amount-label">支付金额</div>
        <div class="amount">¥ {{ amount.toFixed(2) }}</div>
      </div>

      <!-- Order Details -->
      <div class="order-details">
        <div class="order-title">订单信息</div>
        <div class="order-item" v-for="(item, index) in orderItems" :key="index">
          <div class="item-name">{{ item.name }}</div>
          <div class="item-price">¥ {{ item.price.toFixed(2) }}</div>
        </div>
        <div class="order-divider"></div>
        <div class="order-total">
          <div>订单总额</div>
          <div>¥ {{ amount.toFixed(2) }}</div>
        </div>
        <div class="order-number">
          <div>订单编号</div>
          <div>{{ orderNumber }}</div>
        </div>
      </div>

      <!-- Payment Methods -->
      <div class="payment-methods">
        <div class="method-title">选择支付方式</div>
        <div 
          class="payment-method" 
          :class="{ 'selected': selectedMethod === 'wechat' }"
          @click="selectedMethod = 'wechat'"
        >
          <div class="method-icon wechat">
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
              <path d="M9.5,4C5.36,4 2,6.69 2,10C2,11.89 3.08,13.56 4.78,14.66L4,17L6.5,15.5C7.39,15.81 8.37,16 9.41,16C9.15,15.37 9,14.7 9,14C9,10.69 12.13,8 16,8C16.19,8 16.38,8 16.56,8.03C15.54,5.69 12.78,4 9.5,4M6.5,6.5C7.05,6.5 7.5,6.95 7.5,7.5C7.5,8.05 7.05,8.5 6.5,8.5C5.95,8.5 5.5,8.05 5.5,7.5C5.5,6.95 5.95,6.5 6.5,6.5M11.5,6.5C12.05,6.5 12.5,6.95 12.5,7.5C12.5,8.05 12.05,8.5 11.5,8.5C10.95,8.5 10.5,8.05 10.5,7.5C10.5,6.95 10.95,6.5 11.5,6.5M16,9C12.69,9 10,11.24 10,14C10,16.76 12.69,19 16,19C16.67,19 17.31,18.92 17.91,18.75L20,20L19.38,18.13C20.95,17.22 22,15.71 22,14C22,11.24 19.31,9 16,9M14,11.5C14.55,11.5 15,11.95 15,12.5C15,13.05 14.55,13.5 14,13.5C13.45,13.5 13,13.05 13,12.5C13,11.95 13.45,11.5 14,11.5M18,11.5C18.55,11.5 19,11.95 19,12.5C19,13.05 18.55,13.5 18,13.5C17.45,13.5 17,13.05 17,12.5C17,11.95 17.45,11.5 18,11.5Z" />
            </svg>
          </div>
          <div class="method-info">
            <div class="method-name">微信支付</div>
            <div class="method-desc">使用微信扫码支付</div>
          </div>
          <div class="method-check">
            <div class="check-circle" v-if="selectedMethod === 'wechat'"></div>
          </div>
        </div>
        <div 
          class="payment-method" 
          :class="{ 'selected': selectedMethod === 'alipay' }"
          @click="selectedMethod = 'alipay'"
        >
          <div class="method-icon alipay">
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
              <path d="M9.5,4C5.36,4 2,6.69 2,10C2,11.89 3.08,13.56 4.78,14.66L4,17L6.5,15.5C7.39,15.81 8.37,16 9.41,16C9.15,15.37 9,14.7 9,14C9,10.69 12.13,8 16,8C16.19,8 16.38,8 16.56,8.03C15.54,5.69 12.78,4 9.5,4M6.5,6.5C7.05,6.5 7.5,6.95 7.5,7.5C7.5,8.05 7.05,8.5 6.5,8.5C5.95,8.5 5.5,8.05 5.5,7.5C5.5,6.95 5.95,6.5 6.5,6.5M11.5,6.5C12.05,6.5 12.5,6.95 12.5,7.5C12.5,8.05 12.05,8.5 11.5,8.5C10.95,8.5 10.5,8.05 10.5,7.5C10.5,6.95 10.95,6.5 11.5,6.5M16,9C12.69,9 10,11.24 10,14C10,16.76 12.69,19 16,19C16.67,19 17.31,18.92 17.91,18.75L20,20L19.38,18.13C20.95,17.22 22,15.71 22,14C22,11.24 19.31,9 16,9M14,11.5C14.55,11.5 15,11.95 15,12.5C15,13.05 14.55,13.5 14,13.5C13.45,13.5 13,13.05 13,12.5C13,11.95 13.45,11.5 14,11.5M18,11.5C18.55,11.5 19,11.95 19,12.5C19,13.05 18.55,13.5 18,13.5C17.45,13.5 17,13.05 17,12.5C17,11.95 17.45,11.5 18,11.5Z" />
            </svg>
          </div>
          <div class="method-info">
            <div class="method-name">支付宝支付</div>
            <div class="method-desc">使用支付宝扫码支付</div>
          </div>
          <div class="method-check">
            <div class="check-circle" v-if="selectedMethod === 'alipay'"></div>
          </div>
        </div>
      </div>

      <!-- Payment Button -->
      <button class="pay-button" @click="handlePayment">
        立即支付
      </button>

      <!-- QR Code Modal -->
      <div class="qr-modal" v-if="showQRCode">
        <div class="qr-container">
          <div class="qr-header">
            <h2>{{ selectedMethod === 'wechat' ? '微信支付' : '支付宝支付' }}</h2>
            <button class="close-button" @click="showQRCode = false">×</button>
          </div>
          <div class="qr-content">
            <div class="qr-amount">¥ {{ amount.toFixed(2) }}</div>
            <div class="qr-code" :class="selectedMethod">
              <!-- Placeholder for QR code -->
              <div class="qr-placeholder"></div>
            </div>
            <div class="qr-instructions">
              请使用{{ selectedMethod === 'wechat' ? '微信' : '支付宝' }}扫描二维码完成支付
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue';

// Payment data
const amount = ref(299.00);
const orderNumber = ref('202504041234567890');
const orderItems = ref([
  { name: '高级会员（12个月）', price: 299.00 }
]);
const selectedMethod = ref('wechat');
const showQRCode = ref(false);

// Timer functionality
const remainingSeconds = ref(15 * 60); // 15 minutes
let timerInterval = null;

const formatTime = computed(() => {
  const minutes = Math.floor(remainingSeconds.value / 60);
  const seconds = remainingSeconds.value % 60;
  return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
});

const startTimer = () => {
  timerInterval = setInterval(() => {
    if (remainingSeconds.value > 0) {
      remainingSeconds.value--;
    } else {
      clearInterval(timerInterval);
      // Handle payment timeout
      alert('支付超时，请重新下单');
    }
  }, 1000);
};

const handlePayment = () => {
  showQRCode.value = true;
};

onMounted(() => {
  startTimer();
});

onUnmounted(() => {
  if (timerInterval) {
    clearInterval(timerInterval);
  }
});
</script>

<style scoped>
.payment-page {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background-color: #f5f5f5;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 20px;
}

.payment-container {
  max-width: 500px;
  width: 100%;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.header {
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.timer {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #ff4d4f;
}

.timer-icon {
  margin-right: 5px;
  display: flex;
  align-items: center;
}

.amount-display {
  padding: 30px 20px;
  text-align: center;
  background-color: #f9f9f9;
}

.amount-label {
  font-size: 16px;
  color: #666;
  margin-bottom: 10px;
}

.amount {
  font-size: 32px;
  font-weight: 600;
}

.order-details {
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.order-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 15px;
}

.order-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.item-name {
  color: #333;
}

.item-price {
  font-weight: 500;
}

.order-divider {
  height: 1px;
  background-color: #f0f0f0;
  margin: 15px 0;
}

.order-total, .order-number {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 14px;
  color: #666;
}

.payment-methods {
  padding: 20px;
}

.method-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 15px;
}

.payment-method {
  display: flex;
  align-items: center;
  padding: 15px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  margin-bottom: 15px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.payment-method:hover {
  border-color: #e6e6e6;
  background-color: #fafafa;
}

.payment-method.selected {
  border-color: #1890ff;
  background-color: #e6f7ff;
}

.method-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.method-icon.wechat {
  background-color: #07c160;
  color: white;
}

.method-icon.alipay {
  background-color: #1677ff;
  color: white;
}

.method-info {
  flex: 1;
}

.method-name {
  font-weight: 500;
  margin-bottom: 4px;
}

.method-desc {
  font-size: 12px;
  color: #999;
}

.method-check {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.check-circle {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid #1890ff;
  background-color: #1890ff;
  position: relative;
}

.check-circle::after {
  content: "";
  position: absolute;
  width: 6px;
  height: 10px;
  border-right: 2px solid white;
  border-bottom: 2px solid white;
  top: 2px;
  left: 6px;
  transform: rotate(45deg);
}

.pay-button {
  width: calc(100% - 40px);
  margin: 0 20px 20px;
  padding: 12px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.pay-button:hover {
  background-color: #40a9ff;
}

.qr-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.qr-container {
  background-color: white;
  border-radius: 12px;
  width: 90%;
  max-width: 360px;
  overflow: hidden;
}

.qr-header {
  padding: 15px 20px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.qr-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.close-button {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
}

.qr-content {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.qr-amount {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 20px;
}

.qr-code {
  width: 200px;
  height: 200px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.qr-code.wechat::before {
  content: "";
  position: absolute;
  width: 40px;
  height: 40px;
  background-color: #07c160;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.qr-code.alipay::before {
  content: "";
  position: absolute;
  width: 40px;
  height: 40px;
  background-color: #1677ff;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.qr-placeholder {
  width: 180px;
  height: 180px;
  background-image: linear-gradient(
    45deg,
    #f0f0f0 25%,
    transparent 25%,
    transparent 75%,
    #f0f0f0 75%
  ),
  linear-gradient(
    45deg,
    #f0f0f0 25%,
    transparent 25%,
    transparent 75%,
    #f0f0f0 75%
  );
  background-size: 20px 20px;
  background-position: 0 0, 10px 10px;
  border-radius: 4px;
}

.qr-instructions {
  font-size: 14px;
  color: #666;
  text-align: center;
}

@media (max-width: 480px) {
  .payment-page {
    padding: 0;
  }
  
  .payment-container {
    border-radius: 0;
    min-height: 100vh;
  }
  
  .qr-container {
    width: 100%;
    height: 100%;
    max-width: none;
    border-radius: 0;
    display: flex;
    flex-direction: column;
  }
  
  .qr-content {
    flex: 1;
    justify-content: center;
  }
}
</style>